package com.example.blog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.mapper.FriendListMapper;
import com.example.blog.pojo.FriendList;
import com.example.blog.pojo.Result;
import com.example.blog.service.FriendListService;
import com.example.blog.util.TokenUtil.decryptToken;
import com.example.blog.util.TokenUtil.spiltUtils;
import com.example.blog.util.UserUtils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class FriendListServiceImpl extends ServiceImpl<FriendListMapper, FriendList> implements FriendListService {
    @Resource
    public FriendListMapper friendListMapper;
    @Resource
    private UserUtils utils;
    @Override
    public Result<List<FriendList>> getFriendsList(String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        Integer id = utils.getUserId(userName);
        List<FriendList> friendsList = friendListMapper.getFriendsList(id);
        return Result.success(friendsList);
    }

    @Override
    public Result<T> deleteFriendById(int friendId, String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        Integer userId = utils.getUserId(userName);
        Integer id = friendListMapper.getFriendPojo(userId, friendId).getId();
        int delete = friendListMapper.deleteById(id);
        if (delete>0){
            return Result.success("删除成功！");
        }
        return Result.failed("删除失败！");
    }
}
